import { Component } from '@angular/core';

declare var form: any;

@Component({
  templateUrl: './user.component.html'
})
export class ManageUserComponent {

  fs: any = null;
  uafs: any = null;
  ffs: any = null;

  depDic: any = null;
  userTypeDic: any = null;

  // 当前部门选择值
  key: any = null;

  constructor() {
    // 注册表单
    this.fs = new form.FormSimpleService('userinfo', {
      keyField: 'userid',
      randomFields: ['userid'],
      orderObject: { userid: 0 },
      queryFields: 'userid,userName,uImage,organizationId,departmentId,operatingPostId,positionId,roles,employeetype'
    });
    this.fs.loadData(true);
    // 授权表单
    this.uafs = new form.FormSimpleService('userauthentication', {
      keyField: 'userid'
    });
    // 文件服务
    this.ffs = new form.FileService();

    // 初始化字典
    this.depDic = new form.DictionaryService('p_department');
    this.depDic.loadAll();
    this.userTypeDic = new form.DictionaryService('p_user_type');
  }

  // 登录授权
  login(row) {
    const userid = row.userid;
    const uaRow: any = {};
    uaRow.userid = userid;
    this.fs.isLoading = true;
    const ower = this;
    this.uafs.editRowByRemote(uaRow, (data) => {
      ower.fs.isLoading = false;
      ower.uafs.openEdit();
      if (!data || data === null || !data.userid || data.userid === null) {
        ower.uafs.er = uaRow;
      } else {
        ower.uafs.er.password = '';
      }
    });
  }

  loadData() {
    if (this.key === null) {
      this.fs.queryObject['departmentId:like'] = 'nullItem';
    } else {
      this.fs.queryObject['departmentId:like'] = ',' + this.key + ',';
    }
    this.fs.loadData(true);
  }

  editRow(item) {
    if (item) {
      window.open('#users_userinfo_mis?info=true&id=' + item.id);
    } else {
      window.open('#users_userinfo_mis?info=true');
    }
  }
}
